package com.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.util.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by hasee on 2018/6/10.
 */

/**
 * 映射数据面板构建部分
 */
@Controller
@RequestMapping(value = "/DataPanel",produces = "text/html;charset=UTF-8")
public class DataPanelController {
    //映射到列出文件系统根目录
    @RequestMapping(value = "/listRootDir",produces = "text/html;charset=UTF-8")
    public void listRootDir(HttpServletResponse response)throws IOException {
        ArrayList<String> rootDir = FileUtil.listRootDir();
        PrintWriter out= response.getWriter();
        out.print(StringUtils.join(rootDir,","));
        out.flush();
        out.close();
    }

    //映射到列出指定目录下所有文件
    @RequestMapping(value = "/listDirFiles",produces = "text/html;charset=UTF-8")
    public void listDirFiles(HttpServletResponse response,
                            @RequestParam(value = "filePath")String filePath)throws IOException {
        ArrayList<ArrayList<String>> fileList = FileUtil.listNonHideFiles(filePath);
        ArrayList<String> fileNames = fileList.get(0);
        ArrayList<String> isDirs = fileList.get(1);

        HashMap<String,String> fileAttr = new HashMap<>();
        fileAttr.put("fileNames",StringUtils.join(fileNames,"|"));
        fileAttr.put("isDirs",StringUtils.join(isDirs,"|"));
        ObjectMapper mapper = new ObjectMapper();
        PrintWriter out= response.getWriter();
        out.print(mapper.writeValueAsString(fileAttr));
        out.flush();
        out.close();
    }

    //映射到打开指定文件并返回文件信息
    @RequestMapping(value = "/listFilesInfor",produces = "text/html;charset=UTF-8")
    public void listFilesInfor(HttpServletResponse response,
                             @RequestParam(value = "filePath")String filePath)throws IOException {

    }
}
